using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Havivi.PropertyBagSettings;
using Microsoft.SharePoint;

namespace DecryptPropertyBag.UI.WebControls.WebParts
{
    [Guid("b7747bd8-908d-4efd-8f10-138fb8dca84a")]
    public class DecryptPropertyBagWebPartWebPart : Microsoft.SharePoint.WebPartPages.WebPart
    {
        private bool _error = false;


        public DecryptPropertyBagWebPartWebPart()
        {
            this.ExportMode = WebPartExportMode.All;
        }

        /// <summary>
        /// Create all your controls here for rendering.
        /// Try to avoid using the RenderWebPart() method.
        /// </summary>
        protected override void CreateChildControls()
        {
            if (!_error)
            {
                try
                {
                    Guid site = SPContext.Current.Site.ID;
                    Guid web = SPContext.Current.Web.ID;
                     
                    string propertyBagKey = "hello";
                    string propertyBagValueEncrypt = "";
                    string propertyBagValueDecrypt = "";

                    base.CreateChildControls();
                    using (SPSite oSPSite = new SPSite(site))
                    {
                        using (SPWeb oSPWeb = oSPSite.OpenWeb(web))
                        {
                            if (oSPWeb.AllProperties.ContainsKey(propertyBagKey))
                            {
                                propertyBagValueEncrypt = oSPWeb.AllProperties[propertyBagKey].ToString();
                            }
                        }
                    }

                    Havivi.PropertyBagSettings.cTripleDES des = new Havivi.PropertyBagSettings.cTripleDES();
                    propertyBagValueDecrypt = des.Decrypt(propertyBagValueEncrypt);

                    this.Controls.Add(new LiteralControl(propertyBagValueDecrypt));
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Ensures that the CreateChildControls() is called before events.
        /// Use CreateChildControls() to create your controls.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            if (!_error)
            {
                try
                {
                    base.OnLoad(e);
                    this.EnsureChildControls();

                    // Your code here...
                }
                catch (Exception ex)
                {
                    HandleException(ex);
                }
            }
        }

        /// <summary>
        /// Clear all child controls and add an error message for display.
        /// </summary>
        /// <param name="ex"></param>
        private void HandleException(Exception ex)
        {
            this._error = true;
            this.Controls.Clear();
            this.Controls.Add(new LiteralControl(ex.Message));
        }
    }
}
